package pl.edu.mimuw.irs.rcp.controller.editor.workflow;

import org.eclipse.core.expressions.PropertyTester;

import pl.edu.mimuw.irs.core.model.design.net.Fragmentation;
import pl.edu.mimuw.irs.core.model.design.net.IRSNode;
import pl.edu.mimuw.irs.core.model.design.net.IRSPlace;
import pl.edu.mimuw.irs.core.model.design.net.IRSTransition;
import pl.edu.mimuw.irs.core.util.Converter;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.part.IRSNodeEditPart;

public class IRSNodeEditPartTester extends PropertyTester {

	// *** Properties
	
	private static final String IS_PLACE = "isPlace";
	private static final String IS_TRANSITION = "isTransition";
	private static final String CAN_SEQUENCE = "canSequence";
	private static final String CAN_CONCURRENCY = "canConcurrency";
	private static final String CAN_ALTERNATIVE = "canAlternative";
	private static final String CAN_ACTIVITY = "canActivity";
	private static final String CAN_ITERATION = "canIteration";
	private static final String CAN_NONE = "canNone";
	private static final String CAN_EXPAND = "canExpand";
	private static final String CAN_COLLAPSE = "canCollapse";

	// *** Methods

	@Override
	public boolean test(Object receiver, String property, Object[] args,
			Object expectedValue) {
		
		if (!(receiver instanceof IRSNodeEditPart)) return false;
		IRSNodeEditPart editPart = (IRSNodeEditPart) receiver;
		IRSNode node = Converter.asIRSNode(editPart.getNode());

		boolean retval = false;
		if (IS_PLACE.equals(property)) retval = 
			(node instanceof IRSPlace);
		else if (IS_TRANSITION.equals(property)) retval = 
			(node instanceof IRSTransition);
		else if (CAN_SEQUENCE.equals(property)) retval = 
			(node.getSupportedFragmentations().contains(Fragmentation.SEQUENCE) &&
					!(node.getIncomingArcs().size() == 0) &&
					!(node.getOutgoingArcs().size() == 0));
		else if (CAN_CONCURRENCY.equals(property)) retval = 
			(node.getSupportedFragmentations().contains(Fragmentation.CONCURRENCY));
		else if (CAN_ALTERNATIVE.equals(property)) retval = 
			(node.getSupportedFragmentations().contains(Fragmentation.ALTERNATIVE));
		else if (CAN_ACTIVITY.equals(property)) retval = 
			(node.getSupportedFragmentations().contains(Fragmentation.ACTIVITY));
		else if (CAN_ITERATION.equals(property)) retval = 
			(node.getSupportedFragmentations().contains(Fragmentation.ITERATION));
		else if (CAN_NONE.equals(property)) retval = 
			(node.getSupportedFragmentations().contains(Fragmentation.NONE) && !node.isRoot());
		else if (CAN_EXPAND.equals(property)) retval = 
			(!node.isExpanded() && !node.isAtomic());
		else if (CAN_COLLAPSE.equals(property)) retval = 
			(node.getParent() != null && node.getParent().isExpanded());
		
		return retval;
	}
}
